
import edu.princeton.cs.algs4.*;
import java.util.Comparator;

public class Selection {
    private Selection()
    {}
    /* 升序排列 */
    /* 总是从剩余的元素中选择最小的元素，放到已排好序的元素中去 */
    /* ## 运行时间和输入无关 ## */
    /* ## 数据移动是最少的 ##*/
    /* ## 大约需要N^2/2次比较和N次交换 ## */
    public static void sort(Comparable <?> [] a) {
        int n = a.length;

        for(int i = 0; i < n; i++) {
            int min = i;
            /* ## CURRENT INDEX i ## */

            /* Search the mininal ele in the array:a[i+1 ...n] */
            for(int j = i + 1; j < n; j++) {
                if(less(a[j], a[min]))
                    min = j;
            }
            /* exchange the a[i] and the mininal val 
             * in tha array:a[i+1..n] */
            exch(a, i, min);
            assert isSorted(a, 0, i); 
        }
        assert isSorted(a);
    }
    
    private static boolean less(Comparable  v, Comparable   w) {

        return v.compareTo(w) < 0;
        //return lessHelper(v, w);
    }
    /* 
    private static boolean lessHelper(Comparable <?> v, Comparable<?> w)  {

        return v.compareTo(w) < 0;
    }
    */ 
    /* 
    private static boolean lessHelper(Comparator <?> c, Object v, Object w) {

        return c.compare(v, w) < 0;
    }
    */ 
    private static boolean less(Comparator  comparator, Object v, Object w) {

        return comparator.compare(v, w) < 0;
        //return lessHelper(comparator, v, w);
    }

    private static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }

    private static boolean isSorted(Comparable <?> [] a) {
        return isSorted(a, 0, a.length - 1);
    }

    private static boolean isSorted(Comparable <?> [] a, int lo, int hi) {
        for(int i = lo + 1; i <= hi; i++) {
            if(less(a[i], a[i-1]))
                return false;
        }
        return true;
    }
    
    private static boolean isSorted(Object[] a, Comparator comparator) {
        return isSorted(a, comparator, 0, a.length - 1);
    }

    private static boolean isSorted(Object[] a, Comparator comparator, int lo, int hi) {
        for(int i = lo + 1; i <= hi; i++) {
            if(less(comparator, a[i], a[i-1]))
                return false;
        }

        return true;
    }

    private static void show(Comparable[] a) {
        for(int i = 0; i < a.length; i++) {
            StdOut.println(a[i]);
        }
    }

    public static void main(String[] args) {

        String[] a = StdIn.readAllStrings();
        Selection.sort(a);
        show(a);
    }
}
